﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using kim.DBUtility;//Please add references
namespace kim.DAL
{
    /// <summary>
    /// 数据访问类:download_attach
    /// </summary>
    public partial class download_attach
    {
        public download_attach()
        { }
        #region  Method
        public DataSet GetNewsPager(string tbname, string FieldKey, int PageCurrent,
int PageSize, string FieldShow, string FieldOrder, string Where)
        {
            SqlParameter[] parameters ={
               new SqlParameter("@tbname",SqlDbType.VarChar,128),
               new SqlParameter("@FieldKey",SqlDbType.VarChar,1000),
               new SqlParameter("@PageCurrent",SqlDbType.Int),
               new SqlParameter("@PageSize",SqlDbType.Int),
               new SqlParameter("@FieldShow",SqlDbType.NVarChar,1000),
               new SqlParameter("@FieldOrder",SqlDbType.NVarChar,1000),
               new SqlParameter("@Where",SqlDbType.NVarChar,1000),
               new SqlParameter("@ItemCount",SqlDbType.Int),
                new SqlParameter("@PageCount",SqlDbType.Int)};

            parameters[0].Value = tbname;
            parameters[1].Value = FieldKey;
            parameters[2].Value = PageCurrent;
            parameters[3].Value = PageSize;
            parameters[4].Value = FieldShow;
            parameters[5].Value = FieldOrder;
            parameters[6].Value = Where;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;
            return Common.SqlDbHelper.GetDataSetProc("sp_PageView", parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(kim.Model.download_attach model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into download_attach(");
            strSql.Append("article_id,title,file_path,file_ext,file_size,down_num,point,typeid,AddTime,sort)");
            strSql.Append(" values (");
            strSql.Append("@article_id,@title,@file_path,@file_ext,@file_size,@down_num,@point,@typeid,@AddTime,@sort)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@article_id", SqlDbType.Int,4),
					new SqlParameter("@title", SqlDbType.NVarChar,255),
					new SqlParameter("@file_path", SqlDbType.NVarChar,255),
					new SqlParameter("@file_ext", SqlDbType.NVarChar,100),
					new SqlParameter("@file_size", SqlDbType.Int,4),
					new SqlParameter("@down_num", SqlDbType.Int,4),
					new SqlParameter("@point", SqlDbType.Int,4),
					new SqlParameter("@typeid", SqlDbType.Int,4),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
                                        new SqlParameter("@sort",SqlDbType.Int,4)};
            parameters[0].Value = model.article_id;
            parameters[1].Value = model.title;
            parameters[2].Value = model.file_path;
            parameters[3].Value = model.file_ext;
            parameters[4].Value = model.file_size;
            parameters[5].Value = model.down_num;
            parameters[6].Value = model.point;
            parameters[7].Value = model.typeid;
            parameters[8].Value = model.AddTime;
            parameters[9].Value = model.Sort;

            object obj = kim.DBUtility.DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(kim.Model.download_attach model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update download_attach set ");
            strSql.Append("article_id=@article_id,");
            strSql.Append("title=@title,");
            strSql.Append("file_path=@file_path,");
            strSql.Append("file_ext=@file_ext,");
            strSql.Append("file_size=@file_size,");
            strSql.Append("down_num=@down_num,");
            strSql.Append("point=@point,");
            strSql.Append("typeid=@typeid,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("sort=@sort");

            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@article_id", SqlDbType.Int,4),
					new SqlParameter("@title", SqlDbType.NVarChar,255),
					new SqlParameter("@file_path", SqlDbType.NVarChar,255),
					new SqlParameter("@file_ext", SqlDbType.NVarChar,100),
					new SqlParameter("@file_size", SqlDbType.Int,4),
					new SqlParameter("@down_num", SqlDbType.Int,4),
					new SqlParameter("@point", SqlDbType.Int,4),
					new SqlParameter("@typeid", SqlDbType.Int,4),
					new SqlParameter("@AddTime", SqlDbType.DateTime),
					new SqlParameter("@sort", SqlDbType.Int,4),

					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.article_id;
            parameters[1].Value = model.title;
            parameters[2].Value = model.file_path;
            parameters[3].Value = model.file_ext;
            parameters[4].Value = model.file_size;
            parameters[5].Value = model.down_num;
            parameters[6].Value = model.point;
            parameters[7].Value = model.typeid;
            parameters[8].Value = model.AddTime;
            parameters[9].Value = model.Sort;
            parameters[10].Value = model.id;

            int rows = kim.DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from download_attach ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = kim.DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from download_attach ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = kim.DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public kim.Model.download_attach GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,article_id,title,file_path,file_ext,file_size,down_num,point,typeid,AddTime,sort from download_attach ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            kim.Model.download_attach model = new kim.Model.download_attach();
            DataSet ds = kim.DBUtility.DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["article_id"] != null && ds.Tables[0].Rows[0]["article_id"].ToString() != "")
                {
                    model.article_id = int.Parse(ds.Tables[0].Rows[0]["article_id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["title"] != null && ds.Tables[0].Rows[0]["title"].ToString() != "")
                {
                    model.title = ds.Tables[0].Rows[0]["title"].ToString();
                }
                if (ds.Tables[0].Rows[0]["file_path"] != null && ds.Tables[0].Rows[0]["file_path"].ToString() != "")
                {
                    model.file_path = ds.Tables[0].Rows[0]["file_path"].ToString();
                }
                if (ds.Tables[0].Rows[0]["file_ext"] != null && ds.Tables[0].Rows[0]["file_ext"].ToString() != "")
                {
                    model.file_ext = ds.Tables[0].Rows[0]["file_ext"].ToString();
                }
                if (ds.Tables[0].Rows[0]["file_size"] != null && ds.Tables[0].Rows[0]["file_size"].ToString() != "")
                {
                    model.file_size = int.Parse(ds.Tables[0].Rows[0]["file_size"].ToString());
                }
                if (ds.Tables[0].Rows[0]["down_num"] != null && ds.Tables[0].Rows[0]["down_num"].ToString() != "")
                {
                    model.down_num = int.Parse(ds.Tables[0].Rows[0]["down_num"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sort"] != null && ds.Tables[0].Rows[0]["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(ds.Tables[0].Rows[0]["Sort"].ToString());
                }
                if (ds.Tables[0].Rows[0]["point"] != null && ds.Tables[0].Rows[0]["point"].ToString() != "")
                {
                    model.point = int.Parse(ds.Tables[0].Rows[0]["point"].ToString());
                }
                if (ds.Tables[0].Rows[0]["typeid"] != null && ds.Tables[0].Rows[0]["typeid"].ToString() != "")
                {
                    model.typeid = int.Parse(ds.Tables[0].Rows[0]["typeid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddTime"] != null && ds.Tables[0].Rows[0]["AddTime"].ToString() != "")
                {
                    model.AddTime = DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,article_id,title,file_path,file_ext,file_size,down_num,point,typeid,AddTime,Sort ");
            strSql.Append(" FROM download_attach ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return kim.DBUtility.DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,article_id,title,file_path,file_ext,file_size,down_num,point,typeid,AddTime,Sort ");
            strSql.Append(" FROM download_attach ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            if (filedOrder.Trim() != "")
            strSql.Append(" order by " + filedOrder);
            return kim.DBUtility.DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM download_attach ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = kim.DBUtility.DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from download_attach T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return kim.DBUtility.DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "download_attach";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return kim.DBUtility.DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}